package biz

import (
	"context"
	"errors"
)

var (
	errUserNotFound   = errors.New("user not found")
	errLoginFailed    = errors.New("login failed")
	errRegisterFailed = errors.New("register failed")
)

type UserUseCase struct {
	repo UserRepo
}

func NewUserUseCase(repo UserRepo) *UserUseCase {
	return &UserUseCase{
		repo: repo,
	}
}

type User struct {
	Id       int64
	Username string
	Password string
}

type UserRepo interface {
	CreateUser(ctx context.Context, user *User) (*User, error)
	VerifyPassword(ctx context.Context, user *User) (*User, error)
	GetUser(ctx context.Context, id int64) (*User, error)
}

func (uc *UserUseCase) CreateUser(ctx context.Context, user *User) (*User, error) {
	return uc.repo.CreateUser(ctx, user)
}

func (uc *UserUseCase) VerifyPassword(ctx context.Context, user *User) (*User, error) {
	return uc.repo.VerifyPassword(ctx, user)
}

func (uc *UserUseCase) GetUser(ctx context.Context, id int64) (*User, error) {
	return uc.repo.GetUser(ctx, id)
}

func IsUserNotFound(err error) bool {
	if err == nil {
		return false
	}
	return errors.Is(err, errUserNotFound)
}

func ErrorUserNotFound(format string, args ...interface{}) error {
	return errUserNotFound
}

func IsLoginFailed(err error) bool {
	if err == nil {
		return false
	}
	return errors.Is(err, errLoginFailed)
}

func ErrorLoginFailed(format string, args ...interface{}) error {
	return errLoginFailed
}

func IsRegisterFailed(err error) bool {
	if err == nil {
		return false
	}
	return errors.Is(err, errRegisterFailed)
}

func ErrorRegisterFailed(format string, args ...interface{}) error {
	return errRegisterFailed
}
